﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using DFGK.IRepository;
using DFGK.IRepository.BBS;
using DFGK.IService.BBS;
using DFGK_WMS.Model.Models.BBS;

namespace DFGK.Service.BBS
{
    public class BBS_AareService : BaseService, IBBS_AreaService
    {
        private readonly IUnitOfWork _unitOfWork;
        private IBBS_AreaRepositroy _bbs_AreaRepo;
        public BBS_AareService(IMapper mapper, IUnitOfWork unitOfWork, IBBS_AreaRepositroy bbs_AreaRepo) : base(mapper)
        {
            _unitOfWork = unitOfWork;
            _bbs_AreaRepo = bbs_AreaRepo;
        }

        public List<BBS_Area> GetPageData(string areaName, out int totalCount, int pageIndex = 1, int pageSize = 50)
        {
            var query = _bbs_AreaRepo.GetEntityAll();
            if (!string.IsNullOrEmpty(areaName))
            {
                query = query.Where(t => t.AreaName.Contains(areaName));
            }
            totalCount = query.Count();
            query = query.Skip((pageIndex - 1) * pageSize).Take(pageSize).OrderByDescending(o => o.Id);
            return query.ToList();
        }
        public bool SubmitArea(BBS_Area area, out string errorMsg)
        {
            errorMsg = "";
            if (area.Id > 0)
            {
                _bbs_AreaRepo.UpdEntity(area);
            }
            else
            {
                _bbs_AreaRepo.AddEntity(area);
            }
            return _unitOfWork.SaveChangesEntity();
        }

        public bool DelArea(long areaId, out string errorMsg)
        {
            errorMsg = "";
            var area = _bbs_AreaRepo.QueryEntityById(areaId);
            if (area == null)
            {
                errorMsg = "参数错误";
                return false;
            }
            _bbs_AreaRepo.DelEntity(area);
            return _unitOfWork.SaveChangesEntity();
        }

        public BBS_Area? GetArea(long areaId) 
        {
            return _bbs_AreaRepo.QueryEntityById(areaId);
        }
    }
}
